﻿/************************************************************************

 This file is part of "10 Second Tim".

 "10 Second Tim" is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 "10 Second Tim" is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with "10 Second Tim".  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2013 Scott Bevin. all rights reserved

using LDE;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace LudumDare27.GameScreens
{
	internal class IntroSplashScreen : GameScreen
	{
		private const int NumLines = 8;

		private float[ ] Alphas = new float[ NumLines ];
		private float[ ] Timers = { 0.0f, 0.1f, 0.2f, 0.4f, 0.5f, 0.7f, 0.8f, 0.9f, 2.5f };
		private Texture2D[ ] SplashTextures = new Texture2D[ NumLines ];

		private Vector2[ ] Positions = new Vector2[ NumLines ];

		private WhiteFadeScreen whiteFade = null;

		public IntroSplashScreen( )
			: base( true )
		{
		}

		public override void OnAdded( )
		{
			Positions[ 0 ] = new Vector2( 40.0f, 60.0f );
			Positions[ 1 ] = new Vector2( 305.0f, 110.0f );
			Positions[ 2 ] = new Vector2( 190.0f, 135.0f );
			Positions[ 3 ] = new Vector2( 135.0f, 195.0f );
			Positions[ 4 ] = new Vector2( 265.0f, 170.0f );
			Positions[ 5 ] = new Vector2( 100.0f, 250.0f );
			Positions[ 6 ] = new Vector2( 0.0f, 340.0f );
			Positions[ 7 ] = new Vector2( 70.0f, 350.0f );

			for ( int i = 0; i < NumLines; ++i )
			{
				Alphas[ i ] = 0.0f;
				SplashTextures[ i ] = ContentManager.Load<Texture2D>( "Img/IntroScreen/Line" + ( i + 1 ).ToString( ) );
			}

			base.OnAdded( );
		}

		public override void Update( float dt )
		{
			for ( int i = 0; i < NumLines; ++i )
			{
				if ( ( Timers[ i ] -= dt ) <= 0.0f )
				{
					Timers[ i ] = 0.0f;
					Alphas[ i ] += ( dt * 1.0f );

					if ( Alphas[ i ] > 1.0f )
						Alphas[ i ] = 1.0f;
				}
			}

			if ( whiteFade == null && ( Timers[ NumLines ] -= dt ) <= 0.0f )
			{
				this.whiteFade = new WhiteFadeScreen( );
				GameScreenManager.AddGameScreen( whiteFade, null, GameScreenManager.AddGameScreenData.PositioningOptions.OnTop );
			}

			if ( whiteFade != null && whiteFade.State == WhiteFadeScreen.States.FadedIn )
			{
				GameScreenManager.AddGameScreen( new ChooseInputScreen( ), whiteFade, GameScreenManager.AddGameScreenData.PositioningOptions.BelowRef );
				whiteFade.FadeOut( );

				this.KillScreen = true;
			}

			base.Update( dt );
		}

		public override void Render( float dt, GraphicsDevice gd, SpriteBatch sprites )
		{
			Matrix mtx =
				Matrix.CreateTranslation( ( gd.PresentationParameters.BackBufferWidth * 0.5f ), ( gd.PresentationParameters.BackBufferHeight * 0.5f ), 0.0f ) *
				Matrix.CreateScale( 1.0f ) *
				Matrix.CreateRotationZ( 0.0f ) *
				Matrix.CreateTranslation( -320.0f, -240.0f, 0.0f );

			sprites.Begin( SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.LinearClamp,
				DepthStencilState.Default, RasterizerState.CullCounterClockwise, null, mtx );

			for ( int i = 0; i < NumLines; ++i )
			{
				if ( Timers[ i ] <= 0.0f )
				{
					sprites.Draw(
						SplashTextures[ i ],
						Positions[ i ],
						null,
						Color.White * Alphas[ i ] );
				}
			}

			sprites.End( );

			base.Render( dt, gd, sprites );
		}
	}
}